﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WPFTrees.DataStructures
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    // http://codeidol.com/csharp/csharpckbk2/Data-Structures-and-Algorithms/Creating-an-n-ary-Tree/
    public class NTree<T> : IEnumerable<T>
        where T : IComparable<T>
    {
        public NTree()
        {
            maxChildren = int.MaxValue;
        }
        public NTree(int maxNumChildren)
        {
            maxChildren = maxNumChildren;
        }
        
        // The root node of the tree
        protected NTreeNode<T> root = null;
        // The maximum number of child nodes that a parent node may contain
        protected int maxChildren = 0;

        TreeInfo<T> _info;
        public TreeInfo<T> Info
        {
            get { return _info; }
            set { _info = value; }
        }

        public NTreeNode<T> _selectedNode;
        public NTreeNode<T> SelectedNode
        {
            get { return _selectedNode; }
            set { _selectedNode = value; }
        }

        public void AddRoot(NTreeNode<T> node)
        {
            root = node;
        }

        public NTreeNode<T> GetRoot()
        {
            return (root);
        }

        public int MaxChildren
        {
            get { return (maxChildren); }
        }

        IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator()
        {
            List<T> nodes = new List<T>();

            nodes = GetRoot().IterateDepthFirst();
            nodes.Add(GetRoot().Value());

            foreach (T t in nodes)
                yield return (t);
        }

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw (new NotSupportedException("This operation is not " +
    "supported use the GetEnumerator() that returns an IEnumerator<T>."));
        }        
    }


}
